package com.automannn.springboot.practice.bootstrapRegistryInitializer;

import org.springframework.boot.*;
import org.springframework.context.ApplicationListener;

import java.util.Collections;

/**
 * @author chenkh
 * @time 2021/9/30 15:07
 */
/*xxx: 可以往bootstrap中，注册实力提供器，作用未明 */
public class MyBootstrapRegistryInitializerTest implements BootstrapRegistryInitializer {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication();
        springApplication.setSources(Collections.singleton("com.automannn.springboot.practice.normalBean"));
        springApplication.run(args);
    }

    @Override
    public void initialize(BootstrapRegistry registry) {
        registry.addCloseListener(new ApplicationListener<BootstrapContextClosedEvent>() {
            @Override
            public void onApplicationEvent(BootstrapContextClosedEvent event) {
                System.out.println("bootstrap context closing!");
            }
        });

        registry.register(MyBean.class,new MyInstanceSupplier());

      boolean isRegister=  registry.isRegistered(MyInstanceSupplier.class);
        System.out.println(isRegister);
    }

    public static class MyInstanceSupplier implements BootstrapRegistry.InstanceSupplier<MyBean>{

        @Override
        public MyBean get(BootstrapContext context) {
            return new MyBean();
        }
    }

    public static class MyBean{

    }
}
